package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Commentbean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Userbean;
import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Nongfusanquan on 2017-10-10.
 */

public class RegisterActivity extends AppCompatActivity {
    private Button button_reg;
    private EditText username,phone,password,intro;
    private RadioButton man,female;
    private ImageView btn_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        intiView();
        intiEvent();

    }

    private void intiEvent() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://10.0.2.2/yuejian14/index.php/API/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UserService regService = retrofit.create(UserService.class);
                Call call =regService.reg(
                        username.getText().toString(),
                        phone.getText().toString(),
                        password.getText().toString()

                        //confirmpassword.getText().toString(),


                );
                Callback<Commentbean> callback = new Callback<Commentbean>()
                {
                    @Override
                    public void onResponse(Call<Commentbean> call, Response<Commentbean> response) {
                        Commentbean successbean = response.body();
                        if (!"0".equals(String.valueOf(successbean.getRes()))){
                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                            //saveUser(successbean.getUserid());
                            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                        else
                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<Commentbean> call, Throwable t) {

                    }
                };
                call.enqueue(callback);
            }
        });

    }

    private void intiView() {
        username = (EditText)findViewById(R.id.username);
        phone = (EditText)findViewById(R.id.phone);
        password =(EditText)findViewById(R.id.password);
        //confirmpassword = (EditText)findViewById(R.id.confirmpassword);
        intro=(EditText)findViewById(R.id.intro);
        button_reg =(Button)findViewById(R.id.button_reg);
        btn_back=(ImageView)findViewById(R.id.back);
        man = (RadioButton)findViewById(R.id.man);
        female = (RadioButton)findViewById(R.id.female);
    }


}
